package com.example.campusproject.slice;

import com.example.campusproject.ResourceTable;
import com.example.campusproject.adapter.HMBSTListAdapter;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

public class HMBSTAbilitySlice extends AbilitySlice {

    private String[] group={"学院","老师","订餐"};
    private String[][] child={
            {"管理学院\n\n\n\n111111","计算机学院\n\n\n\n222222","软件学院\n\n\n\n333333","理学院\n\n\n\n444444",},
            {"王老师\n\n\n\n000","刘老师\n\n\n\n111","李老师\n\n\n\n222","余老师\n\n\n\n333","赵老师\n\n\n\n444",},
            {"快餐\n\n\n\n01","黄焖鸡\n\n\n\n02","炸鸡\n\n\n\n03",}
    };
    private DirectionalLayout dl;
    private Text[] texts=new Text[group.length];
    private ListContainer[] lcs=new ListContainer[group.length];
    private HMBSTListAdapter[] adapters=new HMBSTListAdapter[group.length];
    private Boolean[] booleans=new Boolean[group.length];
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        this.setUIContent(ResourceTable.Layout_ability_hmbst);
        dl= (DirectionalLayout) this.findComponentById(ResourceTable.Id_dl_hmbst);
        for (int i = 0; i < group.length; i++) {
            booleans[i]=false;
            texts[i]=new Text(this);
            DirectionalLayout.LayoutConfig layoutConfig=
                    new DirectionalLayout.LayoutConfig(
                            ComponentContainer.LayoutConfig.MATCH_PARENT,
                            ComponentContainer.LayoutConfig.MATCH_CONTENT
                    );
            texts[i].setLayoutConfig(layoutConfig);
            texts[i].setText(group[i]);
            texts[i].setTextSize(80);
            texts[i].setTag(i);
            texts[i].setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    int index= (int) ((Text)component).getTag();
                    booleans[index]=!booleans[index];
                    show();
                }
            });
            dl.addComponent(texts[i]);
            lcs[i]=new ListContainer(this);
            lcs[i].setLayoutConfig(layoutConfig);
            adapters[i]=new HMBSTListAdapter(this,child[i]);
            lcs[i].setItemProvider(adapters[i]);
            dl.addComponent(lcs[i]);
            //所有列表全部隐藏
            lcs[i].setVisibility(Component.HIDE);
        }
        booleans[0]=true;
        show();
    }

    public void show(){
        for (int i = 0; i < booleans.length; i++) {
            if (booleans[i])
                lcs[i].setVisibility(Component.VISIBLE);
            else
                lcs[i].setVisibility(Component.HIDE);
        }
    }
}
